package com.taptech.doufu.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.net.retrofit.file.DownLoadApkService;

/**
 * author zhangyongcun
 * 16/11/8
 * 更新提示框
 */

public class DialogUpdateApk extends Dialog implements View.OnClickListener {
    private String updateLog;
    private String updateApkUrl;
    private String updateType;
    private TextView mUpdateLog;
    private Button mNextTimeUpdateBtn;
    private Button mUpdateNowBtn;
    private Context mContext;

    public DialogUpdateApk(@NonNull Context context,String log,String apkUrl,String type,String versionCode) {
        super(context);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.umeng_update_dialog, null);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);

        updateLog = log;
        updateApkUrl = apkUrl;
        updateType = type;

        mUpdateLog = (TextView) view.findViewById(R.id.umeng_update_content);
        mNextTimeUpdateBtn = (Button) view.findViewById(R.id.umeng_update_id_cancel);
        mUpdateNowBtn = (Button) view.findViewById(R.id.umeng_update_id_ok);

        if (updateType != null && updateType.equals("0")) {//选择更新
            mNextTimeUpdateBtn.setVisibility(View.VISIBLE);
        } else if (updateType != null && updateType.equals("1")) {//强制更新
            mNextTimeUpdateBtn.setVisibility(View.INVISIBLE);
        }
        if (updateLog != null) {
            mUpdateLog.setText(updateLog);
        }

        mNextTimeUpdateBtn.setOnClickListener(this);
        mUpdateNowBtn.setOnClickListener(this);
        addContentView(view,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.umeng_update_id_cancel://下次更新
                dismiss();
                break;
            case R.id.umeng_update_id_ok://现在更新
                if (updateApkUrl != null) {
                    Intent intent = new Intent(mContext,DownLoadApkService.class);
                    intent.putExtra(DownLoadApkService.INTENT_KEY_APK_DOWNLOAD_URL, updateApkUrl);
                    mContext.startService(intent);
                }
                dismiss();
                break;
        }

    }

}
